/*
 * RegisterPanel.java
 *
 * Created on 2008年5月28日, 上午11:44
 */

package gui;

import abstractmodel.*;
import javax.swing.JOptionPane;

/**
 *
 * @author  yux
 */
public class RegisterPanel extends javax.swing.JPanel {
    private IModelFactory mf = Main.mf;
    MainFrame frame;
    /** Creates new form RegisterPanel */
    public RegisterPanel(MainFrame f) {
        //s = "register exist";
        frame=f;
        initComponents();
        jLabelEmail.setIcon(source.SourceImage.noImage);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"男", "女"}));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldEmail = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPasswordFieldPw = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        jPasswordFieldPw2 = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jTextFieldPhone = new javax.swing.JTextField();
        jButtonConfirm = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jLabelEmail = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("宋体", 0, 24));
        jLabel1.setText("注册");

        jLabel2.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel2.setText("注册邮箱");

        jLabel5.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel5.setText("必填：");

        jTextFieldEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldEmailActionPerformed(evt);
            }
        });
        jTextFieldEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldEmailKeyTyped(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel3.setText("密码(长度>=6)");

        jLabel4.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel4.setText("确认密码");

        jPasswordFieldPw2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordFieldPw2ActionPerformed(evt);
            }
        });
        jPasswordFieldPw2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jPasswordFieldPw2KeyTyped(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel6.setText("选填:");

        jLabel7.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel7.setText("姓名");

        jLabel8.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel8.setText("性别");

        jComboBox1.setFont(new java.awt.Font("宋体", 0, 14));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel9.setFont(new java.awt.Font("宋体", 0, 14));
        jLabel9.setText("联系电话");

        jButtonConfirm.setFont(new java.awt.Font("宋体", 0, 14));
        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });

        jButtonCancel.setFont(new java.awt.Font("宋体", 0, 14));
        jButtonCancel.setText("取消");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(372, 372, 372)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(243, 243, 243)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(28, 28, 28)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel9)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel3)
                                                .addComponent(jLabel2)))
                                        .addGap(27, 27, 27))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(36, 36, 36)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel4))
                                        .addGap(47, 47, 47)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabelEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jPasswordFieldPw, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jPasswordFieldPw2, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                                    .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(251, 251, 251))
            .addGroup(layout.createSequentialGroup()
                .addGap(284, 284, 284)
                .addComponent(jButtonConfirm)
                .addGap(125, 125, 125)
                .addComponent(jButtonCancel)
                .addContainerGap(290, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(jLabel1)
                .addGap(34, 34, 34)
                .addComponent(jLabel5)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldPw2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(32, 32, 32)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextFieldPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirm)
                    .addComponent(jButtonCancel))
                .addContainerGap(112, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jPasswordFieldPw2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordFieldPw2ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jPasswordFieldPw2ActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here
        this.getParent().remove(this);
    }//GEN-LAST:event_jButtonCancelActionPerformed

    public static String char2string (char[] c) {
        return new String(c);
    }
    
    /** 对邮箱判断 
    */
    private boolean checkEmail (String s) {
        int valid = -1, l = s.length();
        for (int i = 0; i < l; i++) {
            if (s.charAt(i) == '@' && i != 0)    
                valid = 0;
            if (s.charAt(i) == '.' && valid == 0)  
                valid = 1;
        }
        if (valid == 1) {
            //与数据库已有的注册邮箱地址比较，发现相同的提示
            System.out.print("here\n");
            String[] a= this.mf.getAllUserName();
            for(int i=0;i!=a.length;++i) {
                if(a[i].equals(s))
                    return false;
            }
            jLabelEmail.setIcon(source.SourceImage.yesImage);
            return true;
        }
        return false;
    }
    
    /**
     * 检查两次输入的密码是否有效并且一致
     * @return true 仅当两个密码相同且长度有效
     */
    private boolean checkCode () {
        String pw = char2string(jPasswordFieldPw.getPassword());
        String pw2 = char2string(jPasswordFieldPw2.getPassword());
        if (!pw.equals(pw2)) {
            String[] options = {"确定"};
            javax.swing.JOptionPane.showOptionDialog(null, "两次输入的密码不一致！", 
                    "警告", javax.swing.JOptionPane.DEFAULT_OPTION, 
                    javax.swing.JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
            return false;
        }
        if (pw.length() < 6) {
            String[] options = {"确定"};
            javax.swing.JOptionPane.showOptionDialog(null, "密码长度小于6个字符数！", 
                    "警告", javax.swing.JOptionPane.DEFAULT_OPTION, 
                    javax.swing.JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
            return false;
        }
        return true;
    }
    
    /**
     * 
     * @param evt用户按下确定按钮后，收集填入的信息新建一个用户
     * 并且直接登录界面(这部分在MainFrame里面实现)
     */
    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        // TODO add your handling code here:
        if (checkEmail(jTextFieldEmail.getText())==false ) {
            JOptionPane.showMessageDialog(this, "Email 地址不合法");
            return;       
         }else if(this.checkCode()==false) {
            JOptionPane.showMessageDialog(this, "两次密码不一致");
            return;
         }
        
        String password=char2string(jPasswordFieldPw.getPassword());
        System.out.println(password);
        IUser ur =mf.newUser(jTextFieldEmail.getText(), password);
        IInfo uis = ur.getUserInfo();
        uis.setValue("用户名", jTextFieldEmail.getText());
        uis.setValue("密码", char2string(jPasswordFieldPw.getPassword()));
        uis.setValue("客户姓名", jTextFieldName.getText());
        uis.setValue("性别", jComboBox1.getSelectedItem());
        uis.setValue("联系电话", jTextFieldPhone.getText());
        ur.setUserInfo(uis);
        java.awt.CardLayout cl = (java.awt.CardLayout)(this.getParent().getLayout());
        cl.next(this.getParent());
        cl.next(this.getParent());
        frame.setUser(ur);
        frame.setMenuAndTree();
        frame.remove(this);
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    /**
     * 根据邮箱地址框目前的字符串判断这是否是一个有效的邮箱名称
     * 有效地址则将Label的icon置为对的图标，否则，置成错的图标
     * @param evt
     */
    private void jTextFieldEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldEmailActionPerformed
        // TODO add your handling code here:
        String s = jTextFieldEmail.getText();
        System.out.println(s);
        if (!checkEmail(s))
            jLabelEmail.setIcon(source.SourceImage.noImage); 
        else
            jLabelEmail.setIcon(source.SourceImage.yesImage);
    }//GEN-LAST:event_jTextFieldEmailActionPerformed

    private void jTextFieldEmailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldEmailKeyTyped
        // TODO add your handling code here:
        String s = jTextFieldEmail.getText();
        System.out.println(s);
        if (!checkEmail(s))
            jLabelEmail.setIcon(source.SourceImage.noImage); 
        else
            jLabelEmail.setIcon(source.SourceImage.yesImage);
    }//GEN-LAST:event_jTextFieldEmailKeyTyped

    private void jPasswordFieldPw2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldPw2KeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordFieldPw2KeyTyped
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelEmail;
    private javax.swing.JPasswordField jPasswordFieldPw;
    private javax.swing.JPasswordField jPasswordFieldPw2;
    private javax.swing.JTextField jTextFieldEmail;
    private javax.swing.JTextField jTextFieldName;
    private javax.swing.JTextField jTextFieldPhone;
    // End of variables declaration//GEN-END:variables
    
}
